package com.yxhl.zoume.core.user.ui.fragment.identification;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.custom.QuicklyClearEditText;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.user.ui.activity.NameIdentificationActivity;
import com.yxhl.zoume.data.http.rest.param.identification.IdentificationParam;
import com.yxhl.zoume.data.http.rest.response.identification.IdentificationResponse;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.IDNoUtils;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.SoftInputUtils;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * created by sugarya
 * 实名认证 准备提交
 */
public class IdentificationPrepareFragment extends BaseFragment {

    private static final String TAG = "IdentificationPrepareFra";

    @BindView(R.id.qce_identification_prepare_name)
    QuicklyClearEditText mNameQce;

    @BindView(R.id.qce_identification_prepare_card_id)
    QuicklyClearEditText mCardIDQce;

    @BindView(R.id.btn_identification_prepare_submit)
    Button mSubmitBtn;
    IdentificationResponse mIdentificationResponse;
    private String mName;
    private String mCardID;

    public IdentificationPrepareFragment() {
        // Required empty public constructor
    }


    public static IdentificationPrepareFragment newInstance(IdentificationResponse identificationResponse) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIConstants.IDCertify.ARGUMENT_KEY_ID_CERTIFY, identificationResponse);

        IdentificationPrepareFragment fragment = new IdentificationPrepareFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private void parseArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mIdentificationResponse = (IdentificationResponse)
                    bundle.getSerializable(UIConstants.IDCertify.ARGUMENT_KEY_ID_CERTIFY);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        mNameQce.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        parseArguments();
        initParams();
    }

    private void initParams() {
        if (mIdentificationResponse != null) {
            PromptUtils.showShort(getActivity(), mIdentificationResponse.getAuResultInfo());
            mNameQce.setText(mIdentificationResponse.getRealName());
            mCardIDQce.setText(mIdentificationResponse.idCardNo);
        }
    }


    /**
     * 姓名
     */
    @OnTextChanged(R.id.qce_identification_prepare_name)
    void onNameTextChange(CharSequence s, int start, int before, int count) {

        checkCompletedData();
    }

    /**
     * 身份证
     */
    @OnTextChanged(R.id.qce_identification_prepare_card_id)
    void onCardIDTextChange(CharSequence s, int start, int before, int count) {
        if (s.length() != 15) {
            checkCompletedData();
        }
    }

    private void checkCompletedData() {
        mSubmitBtn.setEnabled(false);
        mName = mNameQce.getText().toString().trim();
        mCardID = mCardIDQce.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            mSubmitBtn.setEnabled(false);
            return;
        }

        if (!IDNoUtils.isValidIDNo(mCardID.toUpperCase())) {
            mSubmitBtn.setEnabled(false);
            return;
        }

        mSubmitBtn.setEnabled(true);
    }


    @Override
    protected void initializeInjector() {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_identification_prepare;
    }

    @Override
    protected void setupToolBar() {
        ((NameIdentificationActivity) mActivity).setToolbarTitle(R.string.identification_header_title);
    }

    private void postIdCertify() {
        IdentificationParam param = new IdentificationParam();
        String mobile = AccountUtils.getInstance(mActivity).getAccount().getAccountName();
        String token = AccountUtils.getInstance(mActivity).getAccount().getAccessToken();
        NameIdentificationActivity activity = (NameIdentificationActivity) getActivity();
        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(token)) {
            PromptUtils.showShort(mActivity, R.string.login_dialog_content);
            activity.navigator.navigateToMobileLogin(mActivity);
        } else {
            param.setIdCardNo(mCardID)
                    .setRealName(mName)
                    .setMobile(mobile)
                    .setToken(token);
        }
        activity.postIdCertify(param);
    }

    @OnClick(R.id.btn_identification_prepare_submit)
    public void onClick() {
        postIdCertify();
        SoftInputUtils.closeInputKeyboard(mActivity);
    }

}

